package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.ordermanager.model.PreOrder.Status;
import com.eling.elcms.travel.model.CheckInImplement;
import com.eling.elcms.travel.model.NonMemGroupReserve;
import com.eling.elcms.travel.model.NonMemberGroup;
import com.eling.elcms.travel.model.view.*;
import com.eling.elcms.travel.service.INonMemberGroupManager;

import org.bouncycastle.jce.provider.JDKDSASigner.noneDSA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class NonMemberGroupController extends BaseFormController {

	@Autowired
	private INonMemberGroupManager nonMemGroupManager;

	@RequestMapping("/api/nonmembergroup/save")
	@ResponseBody
	public NonMemberGroup save(@ModelAttribute @ModelFilling NonMemberGroup cond) {
		cond.setStatus(NonMemberGroup.Status.Cancel);
		return nonMemGroupManager.save(cond);
	}

	@RequestMapping("/api/nonmembergroup/{pkNonMemberGroup}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkNonMemberGroup) {
		nonMemGroupManager.remove(pkNonMemberGroup);
		return new Msg("删除成功");
	}

	@RequestMapping("/api/nonmembergroup/query")
	@ResponseBody
	public List<NonMemberGroup> query(@ModelAttribute NonMemberGroup cond) {
		List<NonMemberGroup> query = nonMemGroupManager.query(cond);
		return query;
	}

	@RequestMapping("/api/nonmembergroup/isverifyquery")
	@ResponseBody
	public List<NonMemGroupReserve> isverifyquery(@ModelAttribute NonMemGroupReserve cond
			,@RequestParam(value="nonStatus",required = false)String nonStatus) {
		List<NonMemGroupReserve> list = nonMemGroupManager.isverifyquery(cond);
		List<NonMemGroupReserve> returnList = new ArrayList<>();
		if(nonStatus == null){
			return list;
		}else if("Confirm,Reserve".equals(nonStatus)){
			for (NonMemGroupReserve nonMemGroupReserve : list) {
				if(nonMemGroupReserve.getNonMemberGroup().getStatus() == com.eling.elcms.travel.model.NonMemberGroup.Status.Confirm || 
						nonMemGroupReserve.getNonMemberGroup().getStatus() == com.eling.elcms.travel.model.NonMemberGroup.Status.Reserve){
					returnList.add(nonMemGroupReserve);
				}
			}
		}else {
			for (NonMemGroupReserve nonMemGroupReserve : list) {
				if(nonMemGroupReserve.getNonMemberGroup().getStatus() == com.eling.elcms.travel.model.NonMemberGroup.Status.Effective || 
						nonMemGroupReserve.getNonMemberGroup().getStatus() == com.eling.elcms.travel.model.NonMemberGroup.Status.CheckIn){
					returnList.add(nonMemGroupReserve);
				}
			}
		}
		return returnList;
	}

	@RequestMapping("/api/nonmembergroup/search*")
	@ResponseBody
	public List<NonMemberGroup> search(@ModelAttribute SearchCondition sc, @ModelAttribute NonMemberGroup cond) {
		sc.setQueryCondition(cond);
		return nonMemGroupManager.search(sc);
	}

	@RequestMapping("/api/nonmembergroup/saveall")
	@ResponseBody
	public NonMemGroupReserve saveAll(@ModelAttribute NonMemberGroupView cond) {
		return nonMemGroupManager.saveAll(cond);
	}

	@RequestMapping("/api/nonmembergroup/saveadd")
	@ResponseBody
	public NonMemGroupReserve saveA(@ModelAttribute NonMemberGroupView cond) {
		return nonMemGroupManager.saveADD(cond);
	}

	@RequestMapping("/api/nonmembergroup/queryall")
	@ResponseBody
	public NonMemberGroupView queryAll(@ModelAttribute NonMemberGroup cond) {
		return nonMemGroupManager.queryAll(cond);
	}

	@RequestMapping("/api/nonmembergroup/{pkNonMemberGroup}/deleteall")
	@ResponseBody
	public Msg deleteAll(@PathVariable Long pkNonMemberGroup) {
		return nonMemGroupManager.deleteAll(pkNonMemberGroup);
	}

	@RequestMapping("/api/nonmembergroup/updatestatus")
	@ResponseBody
	public NonMemberGroup updateStatus(@ModelAttribute NonMemberGroup cond) {
		return nonMemGroupManager.updateStatus(cond);
	}

	@RequestMapping("/api/nonmembergroup/pay")
	@ResponseBody
	public NonMemberGroup Pay(@ModelAttribute NonMemberGroup cond) {
		return nonMemGroupManager.Pay(cond);
	}

	@RequestMapping("/api/nonmembergroup/savePay")
	@ResponseBody
	public NonMemberGroup savePay(@ModelAttribute NonMemberGroup cond) {
		return nonMemGroupManager.savePay(cond);
	}

	/**
	 * 散客入住登记
	 * 
	 * @param cond
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/api/nonmembergroup/checkinsave")
	@ResponseBody
	public CheckInImplement checkInSave(@ModelAttribute CheckInImplementView cond,
			@ModelAttribute @ModelFilling CheckInImplement checkInImplement) {
		return nonMemGroupManager.checkInSave(cond, checkInImplement);
	}

	@RequestMapping("/api/nonmembergroup/queryFit")
	@ResponseBody
	public List<MemberGroupView> queryFit(@RequestParam(value = "pkServicePoint") Long pkServicePoint,
			@RequestParam(value = "checkIn") Boolean checkIn, @RequestParam(value = "fstatus") Boolean fstatus,
			@RequestParam(value = "startDate") Date startDate, @RequestParam(value = "endDate") Date endDate) {
		List<MemberGroupView> nonList = nonMemGroupManager.queryFit(pkServicePoint, checkIn, fstatus, startDate,
				endDate);
		return nonList;
	}

	/**
	 * 入住前调整导航条目的地按钮
	 * 
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/nonmembergroup/querydestnandserpoint")
	@ResponseBody
	public List<DestnAndSerPointView> queryDestnAndSerPoint() {
		return nonMemGroupManager.queryDestnAndSerPoint();
	}

	/**
	 * 入住前调整预约散客保存
	 * 
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/nonmembergroup/nonmemsaveadjust")
	@ResponseBody
	public NonMemberGroup nonmemSaveAdjust(@ModelAttribute AdjustGroupView adjustView) {
		return nonMemGroupManager.nonmemSaveAdjust(adjustView);
	}

}
